package njci.softerware.com.myexpress2017.fragement;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnLoadImageListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import njci.softerware.com.myexpress2017.R;
import njci.softerware.com.myexpress2017.activity.CommonScanActivity;
import njci.softerware.com.myexpress2017.activity.MainActivity;
import njci.softerware.com.myexpress2017.utils.Constant;

public class HomeFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

    @Bind(R.id.recevie)
    Button recevie;
    @Bind(R.id.delivery)
    Button delivery;
    @Bind(R.id.sign)
    Button sign;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private Banner banner;
    private Integer postType;
    String[] images = new String[]{"http://img.zcool.cn/community/0192a557a352ef0000012e7ed104e4.jpg", "http://img.zcool.cn/community/01ae5656e1427f6ac72531cb72bac5.jpg"};
    String[] titles = new String[]{"美女", "帅哥"};
    private Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = (MainActivity) getActivity();
        initBanner();
    }


    public void initBanner() {
        banner = (Banner) getActivity().findViewById(R.id.banner);
        //一步搞定，设置图片就行了
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);
        //设置标题列表
        banner.setBannerTitle(titles);
        //设置轮播间隔时间 在布局文件中设置了5秒
        banner.setDelayTime(3000);
        //设置动画
        banner.setBannerAnimation(Transformer.CubeOut);
        banner.setImages(images, new OnLoadImageListener() {
            @Override
            public void OnLoadImage(ImageView view, Object url) {
                /**
                 * 这里你可以根据框架灵活设置
                 */
                Glide.with(getActivity().getApplicationContext())
                        .load(url)
                        .into(view);
            }
        });
    }


    @OnClick({R.id.recevie, R.id.delivery, R.id.sign})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.recevie:
                postType = 0;
                break;
            case R.id.delivery:
                postType = 1;
                break;
            case R.id.sign:
                postType = 2;
                break;
        }

        intent = new Intent(getActivity(), CommonScanActivity.class);
        intent.putExtra(Constant.REQUEST_SCAN_MODE, Constant.REQUEST_SCAN_MODE_ALL_MODE);
        intent.putExtra(Constant.POST_SIGALE, postType);
        startActivity(intent);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i("--", "onStart");
        banner.isAutoPlay(true);
    }

    @Override
    public void onStop() {
        super.onStop();
        super.onStop();
        Log.i("--", "onStop");
        banner.isAutoPlay(false);
    }

}
